<?php
/**
 * Created By PhpStorm
 * Author: patrick wu
 * Date: 2020/10/12
 * Time: 14:17
 */
namespace App\Http\Model\Mysql;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

class CouponModel extends Model
{
    const IS_DELETE =   1,      //已删除
        NOT_DELETE  =   0;      //未删除

    const IS_JOIN   =   1,      //加入领券中心
        NOT_JOIN    =   0;      //不加入领券中心

    const DECREASE_TYPE =   1,      //优惠券类型  满减优惠券
        DISCOUNT_TYPE   =   2;      //折扣优惠券

    const PLATFORM_COUPON   =   1,      //优惠券使用场景  平台优惠券
        SHOP_COUPON     =   2,          //店铺优惠券
        GOOD_COUPON     =   3;          //商品优惠券

    const EXPIRE_DAY    =   1,          //过期天数类型
        EXPIRE_UNTIL    =   2;          //指定时间过期类型

    protected $table    =   'mini_coupon';

    protected $guarded  =   [];

    protected $dateFormat   =   'U';

    /**
     * 领券中心所有优惠券
     * @param $query
     * @param $is_join
     * @return mixed
     */
    public function scopeCenter($query, $is_join = self::IS_JOIN)
    {
        $where['is_delete'] =   self::NOT_DELETE;
        $where['is_join']   =   $is_join;
        $where[]            =   ['total_count', '<', DB::raw('total_num')];
        return $query->where($where);
    }

    /**
     * 定义链接优惠券连接信息表
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function connection()
    {
        return $this->hasMany(CouponConnectionModel::class, 'coupon_id', 'id');
    }

}
